package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.convert.SubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class RadioSubjectTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.getByCode(SubjectTypeEnum.RADI0.getCode());
    }

    @Override
    public boolean add(SubjectInfoBO subjectInfoBO) {
        List<SubjectAnswerBO> subjectAnswerDTOList = subjectInfoBO.getSubjectAnswerDTOList();
        //bo To  entity
        List<SubjectRadio> subjectRadioList = SubjectConverter.INSTANCE.convertDTOToEntity(subjectAnswerDTOList);
        boolean flag = subjectRadioService.insertBath(subjectRadioList);

        return flag;
    }

    @Override
    public List<SubjectAnswerBO> query(Long subjectId) {
        List<SubjectRadio> subjectRadioList = subjectRadioService.queryBySubjectId(subjectId);
        if(subjectRadioList != null && subjectRadioList.size() > 0){
            List<SubjectAnswerBO> subjectAnswerBOS = new ArrayList<>();
            subjectRadioList.forEach(subjectRadio -> {
                SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
                BeanUtils.copyProperties(subjectRadio,subjectAnswerBO);
                subjectAnswerBOS.add(subjectAnswerBO);
            });
            return subjectAnswerBOS;
        }

        return Collections.emptyList();
    }
}
